home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / archie-1.4.1 / stcopy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-18  |  2.0 KB  |  104 lines

  1. /*
  2.  * Copyright (c) 1989, 1990, 1991 by the University of Washington
  3.  *
  4.  * For copying and distribution information, please see the file
  5.  * <copyright.h>.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <pmachine.h>
  10.  
  11. #ifdef NEED_STRING_H
  12. # include <string.h>
  13. #else
  14. # include <strings.h>
  15. #endif
  16.  
  17. #if defined(MSDOS)
  18. # include <stdlib.h>
  19. #endif
  20.  
  21. char    *stcopyr();
  22.  
  23. int    string_count = 0;
  24. int    string_max = 0;
  25.  
  26. /*
  27.  * stcopy - allocate space for and copy a string
  28.  *
  29.  *     STCOPY takes a string as an argument, allocates space for
  30.  *     a copy of the string, copies the string to the allocated space,
  31.  *     and returns a pointer to the copy.
  32.  */
  33.  
  34. char *
  35. stcopy(st)
  36.     char    *st;
  37.     {
  38.       if (!st) return(NULL);
  39.       if (string_max < ++string_count) string_max = string_count;
  40.  
  41.       return strcpy((char *)malloc(strlen(st) + 1), st);
  42.     }
  43.  
  44. /*
  45.  * stcopyr - copy a string allocating space if necessary
  46.  *
  47.  *     STCOPYR takes a string, S, as an argument, and a pointer to a second
  48.  *     string, R, which is to be replaced by S.  If R is long enough to
  49.  *     hold S, S is copied.  Otherwise, new space is allocated, and R is
  50.  *     freed.  S is then copied to the newly allocated space.  If S is
  51.  *     NULL, then R is freed and NULL is returned.
  52.  *
  53.  *     In any event, STCOPYR returns a pointer to the new copy of S,
  54.  *     or a NULL pointer.
  55.  */
  56. char *
  57. stcopyr(s,r)
  58.     char    *s;
  59.     char    *r;
  60.     {
  61.     int    sl;
  62.  
  63.     if(!s && r) {
  64.         free(r);
  65.         string_count--;
  66.         return(NULL);
  67.     }
  68.     else if (!s) return(NULL);
  69.  
  70.     sl = strlen(s) + 1;
  71.  
  72.     if(r) {
  73.         if ((strlen(r) + 1) < sl) {
  74.         free(r);
  75.         r = (char *) malloc(sl);
  76.         }
  77.     }
  78.     else {
  79.         r = (char *) malloc(sl);
  80.         string_count++;
  81.         if(string_max < string_count) string_max = string_count;
  82.     }
  83.         
  84.     return strcpy(r,s);
  85.     }
  86.  
  87. /*
  88.  * stfree - free space allocated by stcopy or stalloc
  89.  *
  90.  *     STFREE takes a string that was returned by stcopy or stalloc 
  91.  *     and frees the space that was allocated for the string.
  92.  */
  93. void
  94. stfree(st)
  95.     char *st;
  96.     {
  97.     if(st) {
  98.         free(st);
  99.         string_count--;
  100.     }
  101.     }
  102.  
  103.  
  104.